'''
Created on Jul 12, 2011

@author: ioan
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Model.Project import Project
from Model.GenericNode import GenericNode
from Model.TreeModel import TreeModel
from View.Widgets.CircularMenu import CircularMenu
from Model.NodeEntity import NodeEntity
from View.Widgets.MiniTextEntryWidget import MiniTextEntryWidget
from Model.Entity import Entity


class SceneExplorerTreeView(QTreeView):
    
    def __init__(self, CircularMenu):
        QTreeView.__init__(self)
        self.mMenu = CircularMenu
        
        
        
    def mousePressEvent(self, QMouseEvent):
        QTreeView.mousePressEvent(self, QMouseEvent)
        if QMouseEvent.button() == Qt.RightButton:
            self.mMenu.spawn(QCursor.pos().x(), QCursor.pos().y())

class SceneExplorerWindow(QDockWidget):
    '''
    The resource explorer view
    '''



    def __init__(self, Scene, mainWindow):
        '''
        Constructor
        '''
        super(SceneExplorerWindow, self).__init__("Scene")
        self.mMainWindow = mainWindow
        self.createCircularMenu()
        self.mTreeView = SceneExplorerTreeView(self.mCircularMenu);
        self.mTreeView.setModel(Scene.mModel)
        self.mScene = Scene
        
        self.mPopupText = MiniTextEntryWidget("NewEntity", self)
        self.mPopupText.mSignalReturned.connect(self.createEntityEntered)

        self.mTreeView.header().hide()
        self.setWidget(self.mTreeView)
        self.mScene.mSignalChanged.connect(self.handle_SceneChanged)
        
    def createEntityEntered(self):
        entityName = self.mPopupText.getText()
        indices = self.mTreeView.selectedIndexes()
        if len(entityName) > 0:
            node = NodeEntity(Entity("/Folder1/" + entityName))
            model = self.mScene.mModel
            nodeIdxInParent = 0
    
            TreeModel.insertRow(model, nodeIdxInParent, indices[0], node)
        
    def createCircularMenu(self):
        
        # Context menu
        self.mCircularMenu      = CircularMenu(180, 10, self)
        self.mEditEntity        = self.mCircularMenu.createTextMenu("Edit entity", 12)
        self.mDeleteEntity        = self.mCircularMenu.createTextMenu("Delete", 12)
        self.mCreateEntity  = self.mCircularMenu.createTextMenu("Create\nentity", 12)
        self.mCircularMenu.mSignalPressed.connect(self.circularMenuPressed)
        
       # Confirmation menu
        self.mConfMenu = CircularMenu(150, 30, self)
        self.mOkMenu = self.mConfMenu.createTextMenu("Ok", 12)
        self.mCancelMenu = self.mConfMenu.createTextMenu("Cancel", 12)
        self.mConfMenu.mSignalPressed.connect(self.circularMenuPressed)
        
    def circularMenuPressed(self, MenuPie):
        '''
        '''
        if MenuPie == self.mEditEntity:
            self.mCircularMenu.hide()
            indices = self.mTreeView.selectedIndexes()
            if len(indices) > 0:
                idx = indices[0]
                node = idx.internalPointer()
                if isinstance(node, NodeEntity):
                    self.mMainWindow.setEntity(node.mEntity)
        elif MenuPie == self.mOkMenu:
            indices = self.mTreeView.selectedIndexes()
            for QModelIndex in indices:
                node = QModelIndex.internalPointer()
                self.mScene.mModel.removeRow(node.getChildIndexInParentNode(), self.mScene.mModel.parent(QModelIndex))
        elif MenuPie == self.mDeleteEntity:
             self.mConfMenu.spawn(QCursor.pos().x(), QCursor.pos().y())
        elif MenuPie == self.mCreateEntity:
            indices = self.mTreeView.selectedIndexes()
            if len(indices) > 0:
                self.mTreeView.expand(indices[0])
                self.mPopupText.spawn(QCursor.pos().x(), QCursor.pos().y())
            
            
            
        

    def handle_SceneChanged(self, GenericNode):
        #self.mTreeView.dataChanged()
        #self.mScene.mModel.updateChange(GenericNode)
        self.mTreeView.setModel(None)
        self.mTreeView.setModel(self.mScene.mModel)

    #def addFolder(self, foldername):
        #self.mModel.reset()
        #self.mRootNode.addChildNode(NodeFolder(foldername))
